﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DA;
using System.Text.RegularExpressions;
using Entities;

namespace BL
{
    public class OrderItemManager
    {
        public ERP_OrderItems GetItem(int ItemId)
        {
            using (dotNetivotEntities model = new dotNetivotEntities())
            {
                return model.ERP_OrderItems.SingleOrDefault(x => x.OrderItemId == ItemId);
            }
        }

        public bool EditOrderItem(ERP_OrderItems orderItem)
        {
            try
            {
                using (dotNetivotEntities model = new dotNetivotEntities())
                {
                    ERP_OrderItems ItemToUpdate = model.ERP_OrderItems.SingleOrDefault(x => x.OrderItemId == orderItem.OrderItemId);
                    ItemToUpdate.OrderItemName = orderItem.OrderItemName;
                    model.SaveChanges();
                }
            }
            catch
            {
                return false;
            }

            return true;
        }

        public bool AddOrderItem(ERP_OrderItems OrderItem)
        {
            try
            {
                using (dotNetivotEntities model = new dotNetivotEntities())
                {
                    model.ERP_OrderItems.AddObject(OrderItem);
                    model.SaveChanges();
                }
            }
            catch
            {
                return false;
            }
            return true;
        }

        public IEnumerable<ERP_OrderItems> GetItems()
        {
            using (dotNetivotEntities model = new dotNetivotEntities())
            {
                return model.ERP_OrderItems.ToList();
            }
        }

        public IEnumerable<ERP_ItemsInOrder> GetItemsInOrder(int orderId)
        {
            using (dotNetivotEntities model = new dotNetivotEntities())
            {
                return model.ERP_ItemsInOrder.Include("ERP_OrderItems").Where(x => x.OrderId == orderId).ToList();
            }
        }

        public bool UpdateOrderItem(ERP_OrderItems OrderItemToUpdate)
        {
            try
            {
                using (dotNetivotEntities model = new dotNetivotEntities())
                {
                    ERP_OrderItems OrderItem = model.ERP_OrderItems.SingleOrDefault(x => x.OrderItemId == OrderItemToUpdate.OrderItemId);
                    OrderItem.OrderItemName = OrderItemToUpdate.OrderItemName;
                    model.SaveChanges();
                }
            }
            catch
            {
                return false;
            }

            return true;
        }

        public bool AddItemToOrder(ERP_ItemsInOrder item)
        {
            try
            {
                using (dotNetivotEntities model = new dotNetivotEntities())
                {
                    model.ERP_ItemsInOrder.AddObject(item);
                    model.SaveChanges();
                }
            }
            catch
            {
                return false;
            }
            return true;
        }
    }
}
